// =====================================================================
//
//  This file is part of the Microsoft Dynamics CRM SDK code samples.
//
//  Copyright (C) Microsoft Corporation.  All rights reserved.
//
//  This source code is intended only as a supplement to Microsoft
//  Development Tools and/or on-line documentation.  See these other
//  materials for detailed information regarding Microsoft code samples.
//
//  THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//
// =====================================================================
namespace Microsoft.Xrm.Sdk.Query
{
    using System.Collections;
    using System.Diagnostics.CodeAnalysis;
    using System.Runtime.Serialization;
    using Microsoft.Xrm.Sdk.Temporary;

	[DataContract(Name = "ConditionExpression", Namespace = XmlNamespaces.V5.Contracts)]
	public sealed class ConditionExpression : IExtensibleDataObject
	{
		public ConditionExpression()
		{
		}

		public ConditionExpression(
			string attributeName,
			ConditionOperator conditionOperator,
			params object[] values)
		{
			_attributeName = attributeName;
			_conditionOperator = conditionOperator;
			if (values != null)
			{
				_values = new DataCollection<object>(values);
			}
		}

		public ConditionExpression(
			string attributeName,
			ConditionOperator conditionOperator,
			object value)
			: this(attributeName, conditionOperator, new object[] { value })
		{
		}

		public ConditionExpression(
			string attributeName,
			ConditionOperator conditionOperator)
			: this(attributeName, conditionOperator, new object[] { })
		{
		}

		/// <summary>
		/// Condition Expression constructor.
		/// </summary>
		/// <param name="attributeName"></param>
		/// <param name="values"></param>
		/// <remarks>Need to handler collections differently. esp. Guid arrays.</remarks>
		public ConditionExpression(
			string attributeName,
			ConditionOperator conditionOperator,
			ICollection values)
		{
			_attributeName = attributeName;
			_conditionOperator = conditionOperator;
			if (values != null)
			{
				_values = new DataCollection<object>();
				foreach (object obj in values)
				{
					_values.Add(obj);
				}
			}
		}

		[DataMember]
		public string AttributeName
		{
			get
			{
				return _attributeName;
			}
			set
			{
				_attributeName = value;
			}
		}

		[DataMember]
		public ConditionOperator Operator
		{
			get
			{
				return _conditionOperator;
			}
			set
			{
				_conditionOperator = value;
			}
		}

		[DataMember]
		public DataCollection<object> Values
		{
			get
			{
				if (_values == null)
				{
					_values = new DataCollection<object>();
				}
				return _values;
			}
			[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Called via reflection")]
			private set
			{
				_values = value;
			}
		}

		[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]
		internal void Accept(IQueryVisitor visitor)
		{
			visitor.Visit(this);
		}

		private string _attributeName;
		private ConditionOperator _conditionOperator;
		private DataCollection<object> _values;

		#region IExtensibleDataObject Implementation
		private ExtensionDataObject ExtensionDataObject;
		public ExtensionDataObject ExtensionData
		{
			get
			{
				return ExtensionDataObject;
			}
			set
			{
				ExtensionDataObject = value;
			}
		}
		#endregion IExtensibleDataObject Implementation
	}
}

